package com.cnhis.cloudhealth.clinical.clidoctor.clinewadvice.bo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.clidoctor.clinewadvice.dao.CliNewAdviceDao;
import com.cnhis.cloudhealth.clinical.util.PiplineUtil;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.util.oplogutil.AsynUtil;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.GlobalVariable;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.MapUtils;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import com.cnhis.cloudhealth.commons.utils.StringUtils;

import net.sf.json.JSONObject;

@SuppressWarnings("rawtypes")
@Service
public class CliNewAdviceBo
{
    @Resource
    private CliNewAdviceDao cliNewAdviceDao;

    @Autowired
    private GetNewId getNewId;
    
    Logger log = Logger.getLogger(CliNewAdviceBo.class);


    public ModelVo getclinicEditOrderBeforeJudge(Map map)
            throws Exception
    {

        ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);

        // 如果是非门诊，则返回成功
        if (!"1".equals(map.get("ACF01").toString()))
        {
            return mo;
        }
        // 是否有限制条件
        if (getBlp02(map))
        {
            return mo;
        }

        // 获取限定年龄
        Integer BLP04 = getBlp04(map);

        // 获取病人年龄
        Integer VAA10 = getVaa10(map);

        if ((VAA10 >= BLP04) && getVcj04Vcj05(map))
        {
            mo.setResult(StaticKeys.OH_FALSE);
            mo.setResultMsg("病人超过" + BLP04 + "岁，必须要输入收缩压和舒张压.");
            return mo;
        }

        return mo;
    }

    /**
     * 入收缩压和舒张压 是否为空
     *
     * @param map
     * @return
     * @throws Exception
     */
    private boolean getVcj04Vcj05(Map map)
            throws Exception
    {
        return cliNewAdviceDao.getVcj04Vcj05(map) > 0;
    }

    /**
     * 获取病人年龄
     *
     * @param map
     * @return
     * @throws Exception
     */
    private Integer getVaa10(Map map)
            throws Exception
    {
        Integer VAA10 = cliNewAdviceDao.getVaa10(map);
        VAA10 = VAA10 == null ? 0 : VAA10;
        return VAA10;
    }

    /**
     * 获取限定年龄
     *
     * @param map
     * @return
     * @throws Exception
     */
    private Integer getBlp04(Map map)
            throws Exception
    {
        Integer BLP04 = cliNewAdviceDao.getBlp04(map);
        BLP04 = BLP04 == null ? 0 : BLP04;
        return BLP04;
    }

    /**
     * 是否有限制条件
     *
     * @param map
     * @return
     * @throws Exception
     */
    private boolean getBlp02(Map map)
            throws Exception
    {
        return cliNewAdviceDao.getBlp02(map) == 0;
    }

    /**
     * 临床药物评价 Clinic_Drug_Judge
     *
     * @param map
     * @return
     * @throws Exception
     */
    public ModelVo clinicDrugJudge(Map map)
            throws Exception
    {

        ModelVo mo = null;

        String lxml = map.get("lxml").toString();
        // 将lxml装换为json
        JSONObject job = JSONObject.fromObject(lxml);

        // 获取医嘱单信息
        List TmpCBM = JsonUtil.parserToList(job.getJSONArray("cbm1"));

        if (TmpCBM.size() == 0)
        {
            return new ModelVo(StaticKeys.OH_FALSE, "医嘱单信息为空");
        }
        // 获取医嘱信息
        List TmpVAF = getTmpVAF(job, TmpCBM);
        if (TmpVAF.size() == 0)
        {
            return new ModelVo(StaticKeys.OH_FALSE, "医嘱信息为空");
        }

        // 获取诊断信息
        List TmpVAO = getTmpVAO(job);

        // 获取病人诊疗申请单信息
        List TmpVBQ = getTmpVBQ(job);

        // 获取病人手术人员信息
        List TmpVAX = getTmpVAX(job);

        String ACF01 = map.get("ACF01").toString();

        String asign = map.get("asign").toString();

        // 修改执行科室ID
        TmpVAF = getTmpVAFByBck01b(TmpVAF, asign);

        // 获取参数
        Map params = getParams(map, ACF01);

        Map cdjMap = new HashMap<>();
        cdjMap.put("tmpCBM", TmpCBM);
        cdjMap.put("tmpVAF", TmpVAF);
        cdjMap.put("tmpVAO", TmpVAO);
        cdjMap.put("tmpVBQ", TmpVBQ);
        cdjMap.put("tmpVAX", TmpVAX);

        List<HashMap> list = new ArrayList<HashMap>();
        HashMap temp = new HashMap();
        list = cliNewAdviceDao.getAllBLPList(temp);
        for (HashMap hashMap : list)
        {
            temp.put("txt", "select " + hashMap.get("BLP07"));
            List ls = cliNewAdviceDao.execProc(temp);
        }

        // 手术医嘱判断
        mo = getJudgeSs(TmpVAF, TmpVAX, params, ACF01);
        if (mo != null)
        {
            return mo;
        }

        // 外配处方，需要整张处方药品执行性质都是外配处方，不能有别的执行性质，请检查.
        if ("1".equals(asign) && "1".equals(ACF01))
        {
            mo = getJudNoTotal(cdjMap);
            if (mo != null)
            {
                return mo;
            }
        }

        // 离院带药，需要处方药品中本组药品执行性质都是离院带药，本组不能有别的执行性质，请检查.
        if ("1".equals(asign))
        {
            mo = getJudLydy(cdjMap);
            if (mo != null)
            {
                return mo;
            }
        }

        // 西成药品医嘱行数
        int tcount = getJudgeXcyCt(TmpVAF);

        // 抗生素类药品用药目的判断
        mo = getJudgeKssYymd(tcount, params, cdjMap, asign);
        if (mo != null)
        {
            return mo;
        }

        // 判断处方诊断
        mo = getJudgeCfPd(tcount, params, TmpCBM, TmpVAO);
        if (mo != null)
        {
            return mo;
        }

        // 只判断西成药品处方
        if (tcount > 0)
        {
            TmpVAF = getJudgeOnlyXcy(cdjMap);
        }
        cdjMap.put("tmpVAF", TmpVAF);
        // 已超出限售金额;超出限售数量
        // 这里是要统计全院的的药品销售情况

        mo = getJudgeExceed(TmpVAF, params, tcount);

        return new ModelVo(StaticKeys.OH_SUCCESS);
    }

    private ModelVo getJudgeExceed(List tmpVAF, Map params, int tcount)
            throws Exception
    {
        if (tcount == 0)
        {
            return null;
        }

        String gpara69 = params.get("gpara69").toString();
        if (gpara69 == null || "0".equals(gpara69))
        {
            return null;
        }

        // 西、成药的限额是否大于1
        boolean xcf = getJEXcf(tmpVAF);

        String adate = params.get("edate").toString().substring(0, 7) + "-01";
        params.put("adate", adate);

        Map map = new HashMap<>();
        map.put("tmpVAF", tmpVAF);
        map.put("adate", adate);
        map.putAll(params);
        ;

        String smsg = null;
        // 已超出限售金额
        if ("1".equals(gpara69) && xcf)
        {
            smsg = cliNewAdviceDao.getJudgeXsje(map);
        }
        // 超出限售数量
        if ("2".equals(gpara69) && xcf)
        {
            smsg = cliNewAdviceDao.getJudgeXssl(map);
        }

        if (smsg != null && (!"".equals(smsg)))
        {
            return new ModelVo(StaticKeys.OH_FALSE, smsg + "，不能再下达此药品.");
        }

        return null;
    }

    private boolean getJEXcf(List tmpVAF)
    {
        Map map;
        String bag46;
        String bda01;
        int l = tmpVAF.size();

        for (int i = 0; i < l; i++)
        {
            map = (Map)tmpVAF.get(i);
            bag46 = MapUtils.getString(map, "bag46");
            bda01 = MapUtils.getString(map, "bda01");
            if (bag46 != null && bda01 != null)
            {
                if (bag46.compareTo("1") > 0 && ("1".equals(bda01) || "2".equals(bda01)))
                {
                    return true;
                }
            }
        }

        return false;
    }

    private List getJudgeOnlyXcy(Map cdjMap)
            throws Exception
    {
        return cliNewAdviceDao.getJudgeOnlyXcy(cdjMap);
    }

    private ModelVo getJudgeCfPd(int tcount, Map params, List tmpCBM, List tmpVAO)
    {
        String mzpara153 = params.get("mzpara153").toString();
        if (tcount > 0 && "1".equals(mzpara153))
        {

            Set set = new HashSet<>();
            Map map = null;
            String cbm06 = null;
            int tc = tmpCBM.size();
            // cbm06 性质: 0:不区分中、西药处方 1=西、成药, 2=中药, 3=手术记录, 4 = 成药,5=卫材, 9=其他
            for (int i = 0; i < tc; i++)
            {
                map = (Map)tmpCBM.get(i);
                cbm06 = MapUtils.getString(map, "cbm06");
                if (cbm06 != null && (!("1".equals(cbm06) || "2".equals(cbm06) || "4".equals(cbm06))))
                {
                    return null;
                }
            }

            String cbmid = null;
            int tv = tmpVAO.size();
            for (int i = 0; i < tv; i++)
            {
                map = (Map)tmpVAO.get(i);
                cbmid = MapUtils.getString(map, "cbmid");
                if (cbmid != null)
                {
                    set.add(cbmid);
                }
            }
            if (set.size() == 0)
            {
                return null;
            }

            for (int i = 0; i < tc; i++)
            {
                map = (Map)tmpCBM.get(i);
                cbmid = MapUtils.getString(map, "cbmid");
                if (set.contains(cbmid))
                {
                    cbm06 = MapUtils.getString(map, "cbm06");
                    if (cbm06 != null && (!("1".equals(cbm06) || "2".equals(cbm06) || "4".equals(cbm06))))
                    {
                        return new ModelVo(StaticKeys.OH_FALSE, "药品处方没有诊断，请填写处方诊断.");
                    }
                }
            }
        }

        return null;
    }

    @SuppressWarnings("unchecked")
    private ModelVo getJudgeKssYymd(int tcount, Map params, Map cdjMap, String asign)
            throws Exception
    {
        String para356_392 = params.get("para356_392").toString();
        cdjMap.put("asign", asign);
        if (tcount > 0 && "1".equals(para356_392))
        {
            String vaf22 = cliNewAdviceDao.getJudgeKssYymd(cdjMap);
            if (vaf22 != null && (!"".equals(vaf22)))
            {
                return new ModelVo(StaticKeys.OH_FALSE, vaf22 + " 没有用药目的，请填写用药目的.");
            }
        }
        return null;
    }

    private int getJudgeXcyCt(List tmpVAF)
    {
        int l = 0;
        int tv = tmpVAF.size();
        Map map = null;
        String bda01 = null;
        for (int i = 0; i < tv; i++)
        {
            map = (Map)tmpVAF.get(i);
            bda01 = MapUtils.getString(map, "bda01");
            if ("1".equals(bda01) || "2".equals(bda01) || "3".equals(bda01))
            {
                l++;
            }

        }
        return l;
    }

    private List getTmpVAX(JSONObject job)
    {
        List ls = new ArrayList();
        if (job.containsKey("vax1"))
        {
            ls = JsonUtil.parserToList(job.getJSONArray("vax1"));
            int ll = ls.size();
            Map map = null;
            for (int i = 0; i < ll; i++)
            {
                map = (Map)ls.get(i);
                map.put("vaf01", 0);
                map.put("vax01", 0);
                map.put("vat01", 0);
            }
        }
        return ls;
    }

    private List getTmpVBQ(JSONObject job)
    {
        List ls = new ArrayList();
        if (job.containsKey("vbq1"))
        {
            ls = JsonUtil.parserToList(job.getJSONArray("vbq1"));
            int ll = ls.size();
            Map map = null;
            for (int i = 0; i < ll; i++)
            {
                map = (Map)ls.get(i);
                map.put("vbqid", 0);
            }
        }
        return ls;
    }

    private List getTmpVAO(JSONObject job)
    {
        List ls = new ArrayList();
        if (job.containsKey("vao1"))
        {
            ls = JsonUtil.parserToList(job.getJSONArray("vao1"));
            int ll = ls.size();
            Map map = null;
            for (int i = 0; i < ll; i++)
            {
                map = (Map)ls.get(i);
                map.put("bak02", "");
            }
        }
        return ls;
    }

    private List getTmpVAF(JSONObject job, List tmpCBM)
    {
        int ll = tmpCBM.size();
        Set set = new HashSet<>();
        String cbmid = null;
        Map map = null;
        for (int i = 0; i < ll; i++)
        {
            map = (Map)tmpCBM.get(i);
            cbmid = MapUtils.getString(map, "cbmid");
            if (cbmid != null)
            {
                set.add(cbmid);
            }
        }

        List rs = new ArrayList<>();

        List ls = JsonUtil.parserToList(job.getJSONArray("vaf1"));
        ll = ls.size();
        String lsign = null;

        for (int i = 0; i < ll; i++)
        {
            map = (Map)ls.get(i);
            lsign = MapUtils.getString(map, "lsign");
            if (lsign != null && "0".equals(lsign))
            {
                cbmid = MapUtils.getString(map, "cbmid");
                if (set.contains(cbmid))
                {
                    map.put("bag46", 0d);
                    map.put("bbt07", 0);
                    map.put("bby23", 0d);
                    rs.add(map);
                }
            }
        }

        return rs;
    }

    private ModelVo getJudLydy(Map map)
            throws Exception
    {
        if (cliNewAdviceDao.getJudLydy(map) > 0)
        {
            return new ModelVo(StaticKeys.OH_FALSE, "离院带药，需要处方药品中本组药品执行性质都是离院带药，本组不能有别的执行性质，请检查.");
        }
        ;
        return null;
    }

    private ModelVo getJudNoTotal(Map map)
            throws Exception
    {
        if (cliNewAdviceDao.getJudNoTotal(map) > 0)
        {
            return new ModelVo(StaticKeys.OH_FALSE, "外配处方，需要整张处方药品执行性质都是外配处方，不能有别的执行性质，请检查.");
        }
        return null;
    }

    private ModelVo getJudgeSs(List tmpVAF, List tmpVAX, Map params, String ACF01)
            throws Exception
    {

        if (!"2".equals(ACF01))
        {
            return null;
        }
        int tv = tmpVAF.size();

        Map map = null;
        String bda01 = null;
        int cnt = 0;
        for (int i = 0; i < tv; i++)
        {
            map = (Map)tmpVAF.get(i);
            bda01 = MapUtils.getString(map, "bda01");
            if (bda01 != null)
            {
                if ("S".equals(bda01) || "0".equals(bda01))
                {
                    cnt++;
                }
            }
        }
        if (cnt == 0)
        {
            return null;
        }

        cnt = getJudgeSsMz(tmpVAF);
        if (cnt == 0)
        {
            return null;
        }

        String zyPara386 = params.get("zypara386").toString();
        Map mls = new HashMap();
        mls.put("tmpVAF", tmpVAF);
        mls.put("tmpVAX", tmpVAX);
        // 没有麻醉方式，请检查.
        if ("1".equals(zyPara386))
        {
            String vaf22 = getJudgeMzfs(mls);
            if (vaf22 != null && !"".equals(vaf22))
            {
                return new ModelVo(StaticKeys.OH_FALSE, vaf22 + "，没有麻醉方式，请检查.");
            }
            // 没有主刀医师，请检查.
            vaf22 = getJudgeNoZd(mls);
            if (vaf22 != null && !"".equals(vaf22))
            {
                return new ModelVo(StaticKeys.OH_FALSE, vaf22 + "，没有主刀医师，请检查.");
            }
        }

        // 没有切口等级，请检查.
        cnt = getBlpBlp0230();
        if (cnt > 0)
        {
            String vaf22 = cliNewAdviceDao.getJudgeNoQkdj(mls);
            if (vaf22 != null && !"".equals(vaf22))
            {
                return new ModelVo(StaticKeys.OH_FALSE, vaf22 + "，没有切口等级，请检查.");
            }
        }

        return null;
    }

    private int getBlpBlp0230()
            throws Exception
    {
        return cliNewAdviceDao.getBlpBlp0230();
    }

    private String getJudgeNoZd(Map mls)
            throws Exception
    {
        return cliNewAdviceDao.getJudgeNoZd(mls);
    }

    private String getJudgeMzfs(Map mls)
            throws Exception
    {
        return cliNewAdviceDao.getJudgeMzfs(mls);
    }

    private int getJudgeSsMz(List tmpVAF)
            throws Exception
    {
        return cliNewAdviceDao.getJudgeSsMz(tmpVAF);
    }

    private Map getParams(Map map, String ACF01)
            throws Exception
    {
        Short isybbr = 0;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Date date = new Date();

        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(Calendar.MONTH, -2);// 减2个月

        Integer type = Integer.valueOf(map.get("type").toString());

        String edate = sdf.format(date);
        String vaj47 = sdf.format(cl.getTime());

        Map param = null;

        // 限制最大销售数量或金额判断
        if ("1".equals(ACF01))
        {
            param = cliNewAdviceDao.getMaxSalePara(map);
            if (Integer.valueOf(param.get("cont").toString()) > 0)
            {
                isybbr = 1;
            }
        }
        if ("2".equals(ACF01))
        {
            if (type.equals(1))
            {
                param = cliNewAdviceDao.getMaxSale21Para(map);
            }
            if (type.equals(2))
            {
                param = cliNewAdviceDao.getMaxSale22Para(map);
            }

            if (Integer.valueOf(param.get("cont").toString()) > 0)
            {
                isybbr = 1;
            }
        }

        param.put("edate", edate);
        param.put("vaj47", vaj47);
        param.put("isybbr", isybbr);

        return param;
    }

    private List getTmpVAFByBck01b(List tmpVAF, String asign)
    {
        if ("0".equals(asign))
        {
            int ll = tmpVAF.size();
            Map map = null;
            String rcurrent = null;
            for (int i = 0; i < ll; i++)
            {
                map = (Map)tmpVAF.get(i);
                rcurrent = MapUtils.getString(map, "rcurrent");
                if (rcurrent != null && "1".equals(rcurrent))
                {
                    map.put("bck01b", 0);
                }
            }
        }
        return tmpVAF;
    }

    //获取诊疗单信息
    //@// Cacheable(value = { "@Cachename1" })
    private List getCviTmpVAO(JSONObject job,List<Map<String,Object>> tmpCBM) {
        if (!job.containsKey("vao1")) {
            return new ArrayList();
        }
        List list = JsonUtil.parserToList(job.getJSONArray("vao1"));
        if(list!=null&&list.size()>0){
            List<Long> vaoids = getNewId.nextId(list.size());
            for (int i = 0; i < list.size(); i++) {
                Map<String,Object> vaoMap = (Map<String,Object>) list.get(i);
                vaoMap.put("vao01",vaoids.get(i));
                String cbmidtc = org.apache.commons.collections.MapUtils.getString(vaoMap,"cbmid");
                for (int j = 0; j < tmpCBM.size(); j++) {
                    Map<String,Object> maptc = (Map<String,Object>) tmpCBM.get(j);
                    String cbmid = org.apache.commons.collections.MapUtils.getString(maptc,"cbmid");
                    if (cbmidtc.equals(cbmid)) {
                        vaoMap.put("cbm01",maptc.get("cbm01"));
                        vaoMap.put("acf01",maptc.get("acf01"));
                        vaoMap.put("vaa01",maptc.get("vaa01"));
                        vaoMap.put("vao07",maptc.get("vao07"));
                        vaoMap.put("vao10",0);
                        vaoMap.put("vao18",0);
                        vaoMap.put("vao22",1);
                    }
                }
            }
        }

        return list;
    }
    
    @Cacheable(value = { "@Cachename1" })
    private Map<String,Object> getCommonParam(Integer ltype,Map<String,Object> map)throws Exception{
        Map<String, Object> rmap = (Map<String, Object>) cliNewAdviceDao.queryForExtObject("Doctorordernew.getCviPara", null);
        String pValue = org.apache.commons.collections.MapUtils.getString(rmap, "pValue"); //系统是否开启全责医师医嘱审核制
        String para203 = org.apache.commons.collections.MapUtils.getString(rmap, "para203"); //开嘱医师与操作员是同一个人时医嘱是否不需要审核
        String gpara50 = org.apache.commons.collections.MapUtils.getString(rmap, "gpara50");
        String mzpara370 = org.apache.commons.collections.MapUtils.getString(rmap, "mzpara370");
        map.put("gpara50",gpara50);
        map.put("mzpara370",mzpara370);
        if(ltype>=1&&ltype<=2) {
            //判断操作员部门和人员业务范围是否为麻醉项目
            Integer cntbce = (Integer)cliNewAdviceDao.queryForExtObject("CliNewAdvice.countDrugedBce", map);
            Integer fChange = cntbce==0?1:0;
            //判断操作员职务是否为士级或待聘以上（等于0是大于0不是）
            Integer cntduty = (Integer)cliNewAdviceDao.queryForExtObject("CliNewAdvice.countDuty", map);
            map.put("pvalue", pValue);
            map.put("fchange", fChange);
            map.put("para203", para203);
            map.put("cntduty", cntduty);
        }else{
            map.put("pvalue", "0");
            map.put("fchange", 0);
            map.put("para203", "0");
        }
        return map;
    }
    /**
     * 获取医嘱内容信息
     *
     * @param job
     * @return
     */
    ///@ //Cacheable(value = { "@Cachename1" })
    private List getCviTmpVAF(JSONObject job, List<Map<String,Object>> tmpCBM,List<Map<String,Object>> tempVBQ,Map<String,Object> map)throws Exception{

        Integer ltype = org.apache.commons.collections.MapUtils.getInteger(map,"ltype");
        getCommonParam(ltype,map);
        String gpara50 = org.apache.commons.collections.MapUtils.getString(map, "gpara50");
        List<Map<String,Object>> list = JsonUtil.parserToList(job.getJSONArray("vaf1"));

        Set<Long> set = new HashSet<Long>();
        Map<String,Object> pmap = new HashMap<String,Object>();

        //生成医嘱单CBM主键
        List<Long> cbmids = getNewId.nextId(tmpCBM.size());
        //组装医嘱单CBM内容
        for (int i = 0; i < tmpCBM.size(); i++) {
            Map<String,Object> cbmMap = (Map<String,Object>)tmpCBM.get(i);
            Long cbmid = org.apache.commons.collections.MapUtils.getLong(cbmMap, "cbmid");
            if (cbmid != null) {
                set.add(cbmid);
            }
            cbmMap.put("cbm01",cbmids.get(i));
            //麻醉、精一药品处方生成编码
            String cbm08 = "";
            if("45".indexOf(GlobalVariable.StringIsNull(cbmMap.get("cbm07")))>=0)
            {
                //更新毒麻处方编号参数值
                cliNewAdviceDao.update("CliNewAdvice.updateP100105002438", null);
                cbm08 = cliNewAdviceDao.getCbm08();
                cbmMap.put("cbm08",cbm08);
            }
            else
            {
                cbmMap.put("cbm08",cbmids.get(i));
            }
            

        }
        //生成医嘱单VBQ主键
        if(tempVBQ!=null&&tempVBQ.size()>0) {
            List<Long> vbqids = getNewId.nextId(tempVBQ.size());
            String ldate = org.apache.commons.collections.MapUtils.getString(map, "ldate");
            ldate = ldate.substring(0, 4);
            for (int i = 0; i < tempVBQ.size(); i++) {
                Map<String, Object> vbqMap = (Map<String, Object>) tempVBQ.get(i);
                String vbq01 = org.apache.commons.collections.MapUtils.getString(vbqMap, "vbq01");
                vbqMap.put("vbq01", vbqids.get(i));
                for (int j = 0; j < tmpCBM.size(); j++) {
                    Map<String, Object> maptc = (Map<String, Object>) tmpCBM.get(j);
                    String cbmid = org.apache.commons.collections.MapUtils.getString(maptc, "cbmid");
                    if (vbq01.equals(cbmid)) {
                        maptc.put("vbq01", vbqids.get(i));
                    }
                }

                vbqMap.put("vbq15", "JC" + ldate + String.format("%010d", vbqids.get(i)));

            }
        }
        //生成医嘱单VAF主键
        List<Long> vafids = getNewId.nextId(list.size());
        Iterator<Map<String,Object>> it = list.iterator();
        int i =0;
        Long vaf01a = 0l;
        while(it.hasNext()){
            Map<String,Object> vafMap = it.next();

            String bda01 = org.apache.commons.collections.MapUtils.getString(vafMap,"bda01");  //诊疗项目类型
            Integer vaf32 = org.apache.commons.collections.MapUtils.getInteger(vafMap,"vaf32"); //0-正常 1-给药途径
            Integer bck01b = org.apache.commons.collections.MapUtils.getInteger(vafMap,"bck01b",0);  //执行科室ID
            Integer bck01d = org.apache.commons.collections.MapUtils.getInteger(vafMap,"bck01d",0);  //给药科室ID
            Integer bby01 = org.apache.commons.collections.MapUtils.getInteger(vafMap,"bby01",0);  //收费项目ID
            Integer vaf11 = org.apache.commons.collections.MapUtils.getInteger(vafMap,"vaf11");    //1-长嘱 2-临嘱
            String vaf23 = org.apache.commons.collections.MapUtils.getString(vafMap,"vaf23","");    //医师嘱托
            Integer vaf27 = org.apache.commons.collections.MapUtils.getInteger(vafMap,"vaf27",0);  //频率次数
            Integer rownr = org.apache.commons.collections.MapUtils.getInteger(vafMap,"rownr");    //序号
            String vaf60 = org.apache.commons.collections.MapUtils.getString(vafMap,"vaf60","0");  //滴速
            Integer vaf17 = org.apache.commons.collections.MapUtils.getInteger(vafMap,"vaf17",0);  //天数

            vafMap.put("vaf01",vafids.get(i));
            if (rownr == 0) {
                vaf01a = vafids.get(i);
                vafMap.put("vaf01a",0l);
            }
            //如果医嘱内容项目为西药（1）/中成药（2）/中草药（3）,更新药品用药标识
            if(("1".equals(bda01)||"2".equals(bda01)||"3".equals(bda01))) {
                Map<String, Object> tempMap = new HashMap<String, Object>();
                tempMap.put("bby01", bby01);
                Map<String, Object> drugMap = (Map<String, Object>) cliNewAdviceDao.queryForExtObject("CliNewAdvice.getdruginfo", tempMap);
                Integer vaf75 = org.apache.commons.collections.MapUtils.getInteger(drugMap, "vaf75",0);
                vafMap.put("vaf75", vaf75);
            }

            //如果医嘱内容项目为治疗（T）和方剂（8），执行性质为给药途径，且执行科室不为空但给药科室为空，则更改给药科室为执行科室
            if(("8".equals(bda01)||"T".equals(bda01))&&vaf32==1&&bck01b>0&&bck01d==0){
                vafMap.put("bck01d",bck01b);
            }
            //如果医嘱内容项目为西药（1）/中成药（2）/中草药（3），且执行科室不为空但给药科室为空，则更改给药科室为执行科室
            if(("1".equals(bda01)||"2".equals(bda01)||"3".equals(bda01))&&bck01b>0&&bck01d==0){
                vafMap.put("bck01d",bck01b);
            }

            //更改医嘱紧急状态（如果为长嘱则更改紧急状态为正常）
            if(vaf11==1){vafMap.put("vaf35",0);}
            //更改频率次数（如果频率次数小于1则默认为1）
            if(vaf27<1){vafMap.put("vaf27",1);}
            //更改滴速（如果项目为西/成药或为治疗项目而且滴速为0则设置滴速为空）
            if(("1".equals(bda01)||"2".equals(bda01))||(vaf32==1 &&"T".equals(bda01))){
                if("0".equals(vaf60)){
                    vafMap.put("vaf60","");
                }
            }
            //如果医嘱内容项目为西药（1）/中成药（2）或者为治疗项目且天数为空或0则默认天数为1
            if(("1".equals(bda01)||"2".equals(bda01)||(vaf32==1 &&"T".equals(bda01)))&&vaf17==0){
                vafMap.put("vaf17",1);
            }
            //如果医嘱内容项目为西药（1）/中成药（2），且医嘱嘱托为皮试
            if(("1".equals(bda01)||"2".equals(bda01))&&vaf23.equals(gpara50)){
                vafMap.put("vaf25","?");
            }else{
                vafMap.put("vaf25","");
            }
            vafMap.put("vaf28", org.apache.commons.collections.MapUtils.getInteger(map,"vaf28",0)); //频率间隔默认值
            //vafMap.put("vaf29", org.apache.commons.collections.MapUtils.getString(map,"vaf29","")); //间隔单位默认值
            vafMap.put("audit_type",0);
            vafMap.put("crypt",0);

            String lsign = org.apache.commons.collections.MapUtils.getString(vafMap,"lsign");
            if (lsign != null && "0".equals(lsign)) {
                String cbmid = org.apache.commons.collections.MapUtils.getString(vafMap, "cbmid");
                for (int j = 0; j < tmpCBM.size(); j++) {
                    Map<String,Object> maptc = (Map<String,Object>) tmpCBM.get(j);
                    String cbmidtc = org.apache.commons.collections.MapUtils.getString(maptc,"cbmid");
                    if (cbmid.equals(cbmidtc)){
                        vafMap.put("cbm01", maptc.get("cbm01"));
                    }
                }
            }
            i++;
        }
        //医嘱相关id赋值
        Iterator<Map<String,Object>> it2 = list.iterator();
        while(it2.hasNext()) {
            Map<String,Object> vafMap = it2.next();
            Integer rownr = org.apache.commons.collections.MapUtils.getInteger(vafMap,"rownr");    //序号
            if (rownr > 0) {
                for(Map<String,Object> temp : list)
                {
                    if("0".equals(temp.get("rownr")+"") && vafMap.get("cbmid").equals(temp.get("cbmid")) && vafMap.get("vaf59").equals(temp.get("vaf59")))
                    {
                        vafMap.put("vaf01a", temp.get("vaf01"));
                    }
                }
            }
        }
        return list;
    }

    //校验医嘱内容信息
   // @//Cacheable(value = { "@Cachename1" })
    private ModelVo validateTmpVAF(List<Map<String,Object>> tmpVAF,Map<String,Object> map)throws Exception{
        Integer lacf01 = org.apache.commons.collections.MapUtils.getInteger(map,"lacf01"); //1-门诊 2-住院
        Integer ltype = org.apache.commons.collections.MapUtils.getInteger(map,"ltype");
        Integer fchange = org.apache.commons.collections.MapUtils.getInteger(map,"fchange");
        String pValue = org.apache.commons.collections.MapUtils.getString(map, "pValue");
        String para203 = org.apache.commons.collections.MapUtils.getString(map,"para203");
        String lbce01 = org.apache.commons.collections.MapUtils.getString(map,"lbce01");
        String ldate = org.apache.commons.collections.MapUtils.getString(map,"ldate");
        Map<String,Integer> rmap = new HashMap<String,Integer>();
        for (int i = 0; i < tmpVAF.size(); i++) {
            Map<String,Object> vafMap = (Map) tmpVAF.get(i);
            String cbmid = org.apache.commons.collections.MapUtils.getString(vafMap,"cbmid");
            String vaf59 = org.apache.commons.collections.MapUtils.getString(vafMap,"vaf59");  //组号
            String vaf36 = org.apache.commons.collections.MapUtils.getString(vafMap,"vaf36"); //获取医嘱开始时间
            String vaf37 = org.apache.commons.collections.MapUtils.getString(vafMap,"vaf37"); //获取长嘱结束时间
            String bda01 = org.apache.commons.collections.MapUtils.getString(vafMap,"bda01");  //诊疗类型
            String vaf26 = org.apache.commons.collections.MapUtils.getString(vafMap,"bda01");  //执行频次
            Integer vaf10 = org.apache.commons.collections.MapUtils.getInteger(vafMap,"vaf10");  //执行状态
            Integer bck01b = org.apache.commons.collections.MapUtils.getInteger(vafMap,"bck01b",0);  //执行科室
            Integer bck01d = org.apache.commons.collections.MapUtils.getInteger(vafMap,"bck01d",0);  //给药科室
            Integer bck01c = org.apache.commons.collections.MapUtils.getInteger(vafMap,"bck01c",0);   //开嘱科室
            Integer vaf32 = org.apache.commons.collections.MapUtils.getInteger(vafMap,"vaf32",0);  //0-正常 1-给药途径
            Integer vaf11 = org.apache.commons.collections.MapUtils.getInteger(vafMap,"vaf11");    //1-长嘱 2-临嘱
            String vaf22 = org.apache.commons.collections.MapUtils.getString(vafMap,"vaf22");  //医嘱内容
            String bce01a = org.apache.commons.collections.MapUtils.getString(vafMap,"bce01a","");  //医嘱内容

            //判断医嘱内容开始时间不能为空
            if (StringUtils.isBlank(vaf36)) {
                return new ModelVo(StaticKeys.OH_FALSE, "医嘱开始时间不能省略！");
            }
            //如果是门诊医嘱，则判断医嘱内容用药频次不能为空
            if(lacf01==1&&("1".equals(bda01)||"2".equals(bda01))&&StringUtils.isBlank(vaf26)){
                return new ModelVo(StaticKeys.OH_FALSE, "医嘱没有用药频次，请修改！");
            }
            //如果是住院医嘱，则判断修改执行状态
            if(lacf01==2&&vaf10>11){
                vafMap.put("audit_type",vaf10-11);
                vafMap.put("vaf10",11);
            }
            //判断执行科室
            if(bck01b==0){
                return new ModelVo(StaticKeys.OH_FALSE, "医嘱执行科室不正确，请检查修改！");
            }
            //判断给药途径对应附加执行科室
            if(bck01d==0&&("8".equals(bda01)||"T".equals(bda01))&&vaf32==1){
                return new ModelVo(StaticKeys.OH_FALSE, "治疗或方剂医嘱执行科室不正确，请检查修改！");
            }
            //判断西成药和种草要附加执行科室
            if(bck01d==0&&("1".equals(bda01)||"2".equals(bda01)||"3".equals(bda01))){
                return new ModelVo(StaticKeys.OH_FALSE, "西/中草药医嘱执行科室不正确，请检查修改！");
            }

            //判断开嘱科室
            if(bck01c==0){
                return new ModelVo(StaticKeys.OH_FALSE, "医嘱开嘱科室不正确，请检查修改！");
            }
            //若是长嘱，则判断结束时间是否正确,若是临嘱则默认给值
            if(vaf11==1){
                if(StringUtils.isBlank(vaf37)){
                    return new ModelVo(StaticKeys.OH_FALSE, "长嘱结束时间不能为空！");
                }
                Date begin = org.apache.commons.lang.time.DateUtils.parseDate(vaf36,new String[]{"yyyy-MM-dd hh:mm:ss"});
                Date end = org.apache.commons.lang.time.DateUtils.parseDate(vaf37,new String[]{"yyyy-MM-dd hh:mm:ss"});
                if(end.before(begin)){
                    return new ModelVo(StaticKeys.OH_FALSE, "长嘱结束时间不能大于开始时间！");
                }
            }else{
                vafMap.put("vaf37","1899-12-30 00:00:00");
            }

            //为审核时间赋值
            if(ltype>=1&&ltype<=2&&"1".equals(pValue)&&1==fchange&&(!"1".equals(para203)||("1".equals(para203)&&!bce01a.equals(lbce01)))){
                vafMap.put("vaf71","1899-12-30 00:00:00");
            }else{
                vafMap.put("vaf71",ldate);
            }
            vafMap.put("vaf38","1899-12-30 00:00:00");  //上次执行时间默认给值
            vafMap.put("vaf51",0);  //申请ID
            vafMap.put("vaf52",0);  //上传标识
            vafMap.put("vaf54",0);  //忽略打印 0-否 1-忽略
            vafMap.put("vaf61", org.apache.commons.collections.MapUtils.getInteger(map,"vaf61",0));  //首日执行次数
            vafMap.put("vaf62", org.apache.commons.collections.MapUtils.getInteger(map,"vaf61",0));  //末日执行次数

//            if(("1".equals(bda01)||"2".equals(bda01)||("T".equals(bda01)&&vaf32==1))){
//                String key = cbmid+vaf59+vaf11;
//                Integer cn = rmap.get(key);
//                if(cn!=null){
//                    return new ModelVo(StaticKeys.OH_FALSE, vaf22+"本组医嘱中存在医嘱期效不一样的情况，请检查.");
//                }else{
//                    rmap.put(key,1);
//                }
//            }
        }

        return null;
    }

    //获取医嘱单信息
    //@//Cacheable(value = { "@Cachename1" })
    private List getCviTmpCBM(JSONObject job, Long lbce01, String lbce03)throws Exception{
        List<Map<String,Object>> list = JsonUtil.parserToList(job.getJSONArray("cbm1"));
        if(list!=null&&list.size()>0) {
            for (int i = 0; i < list.size(); i++) {
                Map<String,Object> cbmMap = (Map<String,Object>)list.get(i);
                Map<String,Object> rmap = (Map<String,Object>)cliNewAdviceDao.queryForExtObject_cacheProxy("CliNewAdvice.getpatientinfo", cbmMap);
                cbmMap.put("vaa04", org.apache.commons.collections.MapUtils.getString(rmap,"vaa04",""));
                cbmMap.put("vaa05", org.apache.commons.collections.MapUtils.getString(rmap,"vaa05",""));
                cbmMap.put("bcq04", org.apache.commons.collections.MapUtils.getString(rmap,"bcq04",""));
                cbmMap.put("bce01", lbce01);
                cbmMap.put("bce03", lbce03);
                cbmMap.put("cbm01", 0);
                cbmMap.put("vbq01", 0);
            }
        }

        return list;
    }
    //插入医嘱单信息
    private void insertCviCBM(Map<String,Object> map) throws Exception {
        cliNewAdviceDao.insert("CliNewAdvice.insertCviCBM", map);
    }
    //插入病人申请单信息
    private void insertCviVbq(Map<String,Object> map) throws Exception {
        if(map.get("tmpVBQ")!=null){
            List<Map<String,Object>> tmpVBQ = (List<Map<String,Object>>)map.get("tmpVBQ");
            if(tmpVBQ.size()>0) {
                cliNewAdviceDao.insert("CliNewAdvice.insertCviVbq", map);
            }
        }
    }
    //插入医嘱内容
    private void insertCviVaf(Map<String,Object> map) throws Exception {
        cliNewAdviceDao.insert("CliNewAdvice.insertCviVaf", map);
    }

    //插入诊断记录
    private void insertCviVao(Map map) throws Exception {
        if(map.get("tmpVAO")!=null){
            List<Map<String,Object>> tmpVAO = (List<Map<String,Object>>)map.get("tmpVAO");
            if(tmpVAO.size()>0) {
                cliNewAdviceDao.insert("CliNewAdvice.insertCviVao", map);
            }
        }
    }
    //插入医嘱状态
    private void insertCviVbg(Map<String,Object> map) throws Exception {
        if(map.get("tmpVAF")!=null){
            List<Map<String,Object>> tmpVAF = (List<Map<String,Object>>)map.get("tmpVAF");
            if(tmpVAF.size()>0) {
                List<Long> vbgids = getNewId.nextId(tmpVAF.size());
                Iterator<Map<String,Object>> it = tmpVAF.iterator();
                int i = 0;
                while(it.hasNext()) {
                    Map<String, Object> vafMap = it.next();
                    vafMap.put("vbgid", vbgids.get(i));
                    i++;
                }
                cliNewAdviceDao.insert("CliNewAdvice.insertCviVbg", map);
            }
        }
    }
    //更新私人诊疗项目
    private void updateCviBdj(Map<String,Object> map) throws Exception {
        Integer lacf01 = org.apache.commons.collections.MapUtils.getInteger(map,"lacf01");
        Integer lmode = org.apache.commons.collections.MapUtils.getInteger(map,"lmode");
        String lbce01 = org.apache.commons.collections.MapUtils.getString(map,"lbce01");
        if(!(lacf01==1&&lmode==2)) {
            List<Map<String, Object>> bdjList = cliNewAdviceDao.queryForList("CliNewAdvice.getCviKbmBDJ", map);
            List<Map<String, Object>> vafList = (List<Map<String, Object>>) map.get("tmpVAF");
            Iterator<Map<String, Object>> it1 = vafList.iterator();
            
            int i = 0, j = 0;
            List<Map<String,Object>> resultMap = new ArrayList<Map<String,Object>>();
            while (it1.hasNext()) {
                Map<String, Object> vafMap = it1.next();
                String bda01 = org.apache.commons.collections.MapUtils.getString(vafMap, "bda01");
                String bby01 = org.apache.commons.collections.MapUtils.getString(vafMap, "bby01");
                String bbx01 = org.apache.commons.collections.MapUtils.getString(vafMap, "bbx01");
                Iterator<Map<String, Object>> it2 = bdjList.iterator();
                while (it2.hasNext()) {
                    Map<String,Object> tmpMap = new HashMap<String,Object>();
                    Map<String, Object> bdjMap = it2.next();
                    String tbbx01 = org.apache.commons.collections.MapUtils.getString(bdjMap, "bbx01","0");
                    String bce01 = org.apache.commons.collections.MapUtils.getString(bdjMap, "bce01","0");
                    Long vaf01 = org.apache.commons.collections.MapUtils.getLong(bdjMap,"vaf01",0l);
                    if ((((bda01.charAt(0) >= '1' && bda01.charAt(0) <= '4') || "M".equals(bda01)) && bby01.equals(tbbx01))
                            || (bda01.charAt(0) >= 'A' && !"M".equals(bda01) && tbbx01.equals(bbx01))) {
                        if (!"0".equals(bda01) && !"8".equals(bda01) && bce01.equals(lbce01)) {
                            tmpMap.put("bda01",bda01);
                            if((bda01.charAt(0) >= '1' && bda01.charAt(0) <= '4') || "M".equals(bda01)) {
                                tmpMap.put("bbx01",bby01);
                            }else if(bda01.charAt(0) >= 'A' && !"M".equals(bda01) ){
                                tmpMap.put("bbx01",bbx01);
                            }
                            if((("E".equals(bda01)||"S".equals(bda01)||"A".equals(bda01))&&vaf01.longValue()>0)){
                                tmpMap.put("vaf01",0l);
                            }else{
                                tmpMap.put("vaf01",vaf01);
                            }
                        }
                    }
                    if(!tmpMap.isEmpty()) {
                        resultMap.add(tmpMap);
                    }
                    j++;
                }
                i++;
            }
            Map<String,String> lMap = new HashMap<String,String>();
            if(resultMap.size()>0) {
                Iterator<Map<String, Object>> it3 = resultMap.iterator();
                while(it3.hasNext()){
                    Map<String,Object> rmap = it3.next();
                    String bda01 = org.apache.commons.collections.MapUtils.getString(rmap,"bda01");
                    String bbx01 = org.apache.commons.collections.MapUtils.getString(rmap,"bbx01");
                    Long vaf01 = org.apache.commons.collections.MapUtils.getLong(rmap,"vaf01");
                    String key = bda01+"-"+bbx01;
                    String value = vaf01+"-"+1;
                    if(lMap.get(key)!=null){
                        String lvalue = lMap.get(key);
                        String larr[] = lvalue.split("-");
                        String lvaf01 = larr[0];
                        String fnum = larr[1];
                        Long max = Long.valueOf(lvaf01);
                        Integer fn = Integer.valueOf(fnum);
                        fn +=1;
                        if(vaf01.longValue()>max.longValue()){
                            lMap.put(key,vaf01+"-"+fn);
                        }else{
                            lMap.put(key,max+"-"+fn);
                        }
                    }else{
                        lMap.put(key,value);
                    }

                }
            }

            if(lMap.size()>0){
                List<Map<String,Object>> plist = new ArrayList<Map<String,Object>>();

                for (Map.Entry<String, String> entry : lMap.entrySet()) {
                    Map<String,Object> tmap = new HashMap<String,Object>();
                    String key = entry.getKey();
                    String value = entry.getValue();
                    String bda01 = key.split("-")[0];
                    String bbx01 = key.split("-")[1];
                    String vaf01 = value.split("-")[0];
                    String fnum = value.split("-")[1];
                    tmap.put("bda01",bda01);
                    tmap.put("bbx01",Integer.valueOf(bbx01));
                    tmap.put("vaf01",Long.valueOf(vaf01));
                    tmap.put("vaf01",Long.valueOf(vaf01));
                    tmap.put("fnum",Integer.valueOf(fnum));
                    tmap.put("lbce01",lbce01);
                    plist.add(tmap);
                }
                if(plist.size()>0){
                    Iterator<Map<String,Object>> it3 = plist.iterator();
                    while(it3.hasNext()){
                        Map<String,Object> tmap =  it3.next();
                        cliNewAdviceDao.update("CliNewAdvice.updateCviBdj",tmap);
                    }
                }
            }
        }
    }
    private boolean existsBdj(Map<String,Object> map,List<Map<String,Object>> bdjList,String bda01,String bbx01,String bby01,String lbce01)throws Exception{
        Iterator<Map<String, Object>> it = bdjList.iterator();
        boolean flag = false;
        while (it.hasNext()) {

            Map<String, Object> bdjMap = it.next();
            String tbbx01 = org.apache.commons.collections.MapUtils.getString(bdjMap, "bbx01","0");
            String bce01 = org.apache.commons.collections.MapUtils.getString(bdjMap, "bce01","0");
            String bda011 = org.apache.commons.collections.MapUtils.getString(bdjMap, "bda01","0");
            if (((((bda01.charAt(0) >= '1' && bda01.charAt(0) <= '4') || "M".equals(bda01)) && tbbx01.equals(bby01))
                    || (bda01.charAt(0) >= 'A' && !"M".equals(bda01) && tbbx01.equals(bbx01))) && bce01.equals(lbce01) && bda01.equals(bda011)) {
                flag = true;
            }

        }
        return flag;
    }

    //插入私人诊疗项目
    private void insertCviBdj(Map<String,Object> map) throws Exception {
        Integer lacf01 = org.apache.commons.collections.MapUtils.getInteger(map, "lacf01");
        Integer lmode = org.apache.commons.collections.MapUtils.getInteger(map, "lmode");
        String lbce01 = org.apache.commons.collections.MapUtils.getString(map, "lbce01");
        if (!(lacf01 == 1 && lmode == 2)) {
            List<Map<String, Object>> bdjList = cliNewAdviceDao.queryForList("CliNewAdvice.getCviKbmBDJ", map);
            List<Map<String, Object>> vafList = (List<Map<String, Object>>) map.get("tmpVAF");
            Iterator<Map<String, Object>> it1 = vafList.iterator();
            int i = 0, j = 0;
            List<Map<String, Object>> resultMap = new ArrayList<Map<String, Object>>();
            while(it1.hasNext()){
                Map<String, Object> vafMap = it1.next();
                String bda01 = org.apache.commons.collections.MapUtils.getString(vafMap, "bda01");
                String bby01 = org.apache.commons.collections.MapUtils.getString(vafMap, "bby01");
                String bbx01 = org.apache.commons.collections.MapUtils.getString(vafMap, "bbx01");
                Long vaf01 = org.apache.commons.collections.MapUtils.getLong(vafMap, "vaf01",0l);
                Long vaf01a = org.apache.commons.collections.MapUtils.getLong(vafMap, "vaf01a",0l);
                if(!"0".equals(bda01) && !"8".equals(bda01)){
                    Map<String, Object> tmpMap = new HashMap<String, Object>();
                    boolean flag = existsBdj(map,bdjList,bda01,bbx01,bby01,lbce01);
                    if (!flag) {
                        tmpMap.put("bda01", bda01);
                        if((("E".equals(bda01)||"S".equals(bda01)||"A".equals(bda01))&&vaf01a>0)||"3".equals(bda01)){
                            vaf01 = 0l;
                        }
                        tmpMap.put("vaf01", vaf01);
                        if ((bda01.charAt(0) >= '1' && bda01.charAt(0) <= '4') || "M".equals(bda01)) {
                            tmpMap.put("bbx01", bby01);
                        } else if (bda01.charAt(0) >= 'A' && !"M".equals(bda01)) {
                            tmpMap.put("bbx01", bbx01);
                        }
                    }
                    if (!tmpMap.isEmpty()) {
                        resultMap.add(tmpMap);
                    }
                }
            }
            Map<String, String> lMap = new HashMap<String, String>();
            if (resultMap.size() > 0) {
                Iterator<Map<String, Object>> it2 = resultMap.iterator();
                while (it2.hasNext()) {
                    Map<String, Object> rmap = it2.next();
                    String bda01 = org.apache.commons.collections.MapUtils.getString(rmap, "bda01");
                    String bbx01 = org.apache.commons.collections.MapUtils.getString(rmap, "bbx01");
                    Long vaf01 = org.apache.commons.collections.MapUtils.getLong(rmap, "vaf01");
                    String key = bda01 + "-" + bbx01;
                    String value = vaf01 + "-" + 1;
                    if (lMap.get(key) != null) {
                        String lvalue = lMap.get(key);
                        String larr[] = StringUtils.split(lvalue,"-");
                        String lvaf01 = larr[0];
                        String fnum = larr[1];
                        Long max = Long.valueOf(lvaf01);
                        Integer fn = Integer.valueOf(fnum);
                        fn += 1;
                        if (vaf01.longValue() > max.longValue()) {
                            lMap.put(key, vaf01 + "-" + fn);
                        } else {
                            lMap.put(key, max + "-" + fn);
                        }
                    } else {
                        lMap.put(key, value);
                    }

                }
            }

            if (lMap.size() > 0) {
                List<Map<String, Object>> plist = new ArrayList<Map<String, Object>>();
                for (Map.Entry<String, String> entry : lMap.entrySet()) {
                    Map<String, Object> tmap = new HashMap<String, Object>();
                    String key = entry.getKey();
                    String value = entry.getValue();
                    String bda01 = key.split("-")[0];
                    String bbx01 = key.split("-")[1];
                    String vaf01 = value.split("-")[0];
                    String fnum = value.split("-")[1];
                    tmap.put("bdj01", getNewId.nextId());
                    tmap.put("bda01", bda01);
                    tmap.put("bbx01", Integer.valueOf(bbx01));
                    tmap.put("vaf01", Long.valueOf(vaf01));
                    tmap.put("vaf01", Long.valueOf(vaf01));
                    tmap.put("bdj04", Integer.valueOf(fnum));
                    tmap.put("lbce01", lbce01);
                    tmap.put("bck01", 0);
                    plist.add(tmap);
                }
                if (plist.size() > 0) {
                    map.put("tmpBDJ", plist);
                    cliNewAdviceDao.insert("CliNewAdvice.insertCviBdj", map);
                }
            }
        }
    }
    //获取手术人员信息
   // @//Cacheable(value = { "@Cachename1" })
    private List getCviTmpVAX(JSONObject job) {
        if (!job.containsKey("vax1")) {
            return new ArrayList();
        }
        List ls = JsonUtil.parserToList(job.getJSONArray("vax1"));
        int l = ls.size();
        Map map = null;
        for (int i = 0; i < l; i++) {
            map = (Map) ls.get(i);
            map.put("vaf01", 0);
            map.put("vax01", 0);
            map.put("vat01", 0);
        }
        return ls;
    }
    //插入手术人员信息和医嘱附件
    private void insertCviVax(Map<String,Object> map) throws Exception {
        List<Map<String,Object>> tmpVAX = (List<Map<String,Object>>)map.get("tmpVAX");
        List<Map<String,Object>> tmpVBF = (List<Map<String,Object>>)map.get("tmpVBF");
        List<Map<String, Object>> vaxList = new ArrayList<Map<String, Object>>();
        if(tmpVAX!=null&&tmpVAX.size()>0) {
            int tvx = tmpVAX.size();
            for (int i = 0; i < tvx; i++) {
                Map<String, Object> vaxMap = (Map<String, Object>) tmpVAX.get(i);
                String ace01 = org.apache.commons.collections.MapUtils.getString(vaxMap, "ace01");
                Long vaf01 = org.apache.commons.collections.MapUtils.getLong(vaxMap, "vaf01");
                if (StringUtils.isNotBlank(ace01) && (!"--".equals(ace01)) && (vaf01 > 0)) {
                    vaxList.add(vaxMap);
                }
            }
        }
        if (vaxList.size() > 0) {
            map.put("tmpVAX", vaxList);
            cliNewAdviceDao.insert("CliNewAdvice.insertCviVax", map);
        }
        if(tmpVBF!=null&&tmpVBF.size()>0) {
            int tvf = tmpVBF.size();
            if (tvf > 0) {
                cliNewAdviceDao.insert("CliNewAdvice.insertCviVbf", map);
            }
        }
    }
    private void updateCviVcj1(Map<String,Object> map) throws Exception {
        List<Map<String,Object>> tmpCBM = (List<Map<String,Object>>)map.get("tmpCBM");
        //医嘱插入目前只针对一个病人，所以取一个病人的挂号ID即可
        Long vaa07 = 0l;
        int tc = tmpCBM.size();
        for (int i = 0; i < tc; i++) {
            Map cbmMap = (Map) tmpCBM.get(i);
            String vaa07s = org.apache.commons.collections.MapUtils.getString(cbmMap, "vaa07");
            if (StringUtils.isNotBlank(vaa07s)) {
                vaa07 = Long.valueOf(vaa07s);
                break;
            }
        }

        //查看病人挂号附表中是否有该病人
        map = new HashMap();
        map.put("vaa07",vaa07);
        map.put("ldate", org.apache.commons.collections.MapUtils.getString(map,"ldate"));
        Integer ct = (Integer)cliNewAdviceDao.queryForExtObject("CliNewAdvice.getCviHasVaa07", map);
        if(ct>0){
            cliNewAdviceDao.update("CliNewAdvice.updateCviVcj1", map);
        }
        else{
            cliNewAdviceDao.insert("CliNewAdvice.insertCviVcj1", map);
        }

        String mzpara370 = org.apache.commons.collections.MapUtils.getString(map,"mzpara370");
        if(mzpara370!=null&&"1".equals(mzpara370)){
            cliNewAdviceDao.update("CliNewAdvice.updateCviFromMzpara370", map);
        }
    }
    //更新病人手术人员信息，获取病人医嘱附件
    private List getCviTmpVBF(List<Map<String,Object>> tmpVAX, List<Map<String,Object>> tmpVAF, List vbfList) {
        int tvx = tmpVAX.size();
        if (tvx == 0) {
            return tmpVAX;
        }

        int tvf = tmpVAF.size();
        // 更新病人手术人员信息ID
        List<Long> ls = getNewId.nextId(tvx);
        for (int i = 0; i < tvx; i++) {
            Map<String,Object> mapx = (Map<String,Object>) tmpVAX.get(i);
            mapx.put("vax01", ls.get(i));
            String cbmid = org.apache.commons.collections.MapUtils.getString(mapx,"cbmid");
            String vaf59 = org.apache.commons.collections.MapUtils.getString(mapx,"vaf59");
            String rownr = org.apache.commons.collections.MapUtils.getString(mapx,"rownr");
            for (int j = 0; j < tvf; j++) {
                Map<String,Object> mapf = (Map) tmpVAF.get(j);
                String cbmidr = org.apache.commons.collections.MapUtils.getString(mapx,"cbmid");
                String vaf59r = org.apache.commons.collections.MapUtils.getString(mapx,"vaf59");
                String rownrr = org.apache.commons.collections.MapUtils.getString(mapx,"rownr");
                if (cbmid.equals(cbmidr) && vaf59.equals(vaf59r) && rownr.equals(rownrr)) {
                    mapx.put("vaf01", mapf.get("vaf01"));
                    break;
                }

            }
        }

        //获取病人医嘱附件信息
        List<Map<String,Object>> tmpVBF = new ArrayList<Map<String,Object>>();
        for (int i = 0; i < vbfList.size(); i++) {
            Map<String,Object> mapx = (Map<String,Object>)vbfList.get(i);
            String ace01 = org.apache.commons.collections.MapUtils.getString(mapx,"ace01");
            if ( "--".equals(ace01)) {
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("vaf01", (Map) tmpVAF.get(0).get("vaf01"));
                map.put("bce02", mapx.get("bce02"));
                map.put("bce03", mapx.get("bce03"));
                tmpVBF.add(map);
            }
        }

        int tvbf = tmpVBF.size();
        if (tvbf == 0) {
            return tmpVBF;
        }
        //更新病人医嘱附件ID
        ls = getNewId.nextId(tvbf);
        for (int i = 0; i < tvbf; i++) {
            Map<String,Object> map = (Map<String,Object>)tmpVBF.get(i);
            map.put("vbf01", ls.get(i));
        }
        return tmpVBF;
    }
    /**
     * 新开医嘱(住院) Clinic_VAF1_Insert
     *
     * @param map
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public ModelVo insertClinicVAF1(Map map) throws Exception {
        ModelVo mo = new ModelVo();
        try
        {
            String  rcbm = GlobalVariable.StringIsNull(map.get("rcbm"));
            log.info("-------------------------------------------智慧医保和数据校验耗时"+rcbm+"毫秒--------------------------------------");
            log.info("----------------------------------------------保存医嘱开始------------------------------------------------------");
            long l1 = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String ldate = sdf.format(new Date());
        map.put("ldate", ldate);
        Long lbce01 = org.apache.commons.collections.MapUtils.getLong(map,"lbce01"); //员工ID
        String lbce03 = org.apache.commons.collections.MapUtils.getString(map,"lbce03");  //员工姓名
        String lxml = org.apache.commons.collections.MapUtils.getString(map,"lxml"); //获取医嘱相关json信息字符串
        if(StringUtils.isBlank(lxml)){
            return new ModelVo(StaticKeys.OH_FALSE, "提交信息错误，医嘱相关信息为空！");
        }
        JSONObject job = JSONObject.fromObject(lxml);
        if(job==null||job.isEmpty()){
            return new ModelVo(StaticKeys.OH_FALSE, "提交信息错误，医嘱相关信息格式不正确，请检查！");
        }
        
        //ati ticye 
        //List<Map<String,Object>> tmpVBQ = getCviTmpVBQ(job); // 获取病人诊疗申请单
        
        
        List<Map<String,Object>> tmpCBM = getCviTmpCBM(job, lbce01, lbce03);  // 获取医嘱单信息
        if (tmpCBM==null||tmpCBM.size() == 0) {
            return new ModelVo(StaticKeys.OH_FALSE, "提交信息错误，医嘱单信息不能为空！");
        }
       
        List<Map<String,Object>> tmpVBQ = getCviTmpVBQ(job); // 获取病人诊疗申请单  jeig not db io
        List<Map<String,Object>> tmpVAF = getCviTmpVAF(job,tmpCBM,tmpVBQ,map); // 获取医嘱内容信息
        if (tmpVAF!=null && tmpVAF.size() == 0) {
            return new ModelVo(StaticKeys.OH_FALSE, "提交信息错误，医嘱内容信息不能为空！");
        }
        List tmpVAO=(List) getCviTmpVAO (job,tmpCBM); // 获取病人诊断信息  menjen binren jendwe jilu  jieg bsi db neiron ,only mem ,not need cache
      
        List tmpVAX = getCviTmpVAX(job);// 获取病人手术人员信息  nocache memcalc
        List vbfList = getvbfList(job);// 获取病人手术人员信息 nocache memcalc
        List tmpVBF = getCviTmpVBF(tmpVAX, tmpVAF,vbfList);// 更新病人手术人员信息，获取病人医嘱附件
        ModelVo mvo = validateTmpVAF(tmpVAF,map);//后台校验医嘱内容 nocache memcalc
        if(mvo!=null){
            return mvo;
        }
        // 若是修改医嘱则先删除修改的医嘱单
        Integer lmode = org.apache.commons.collections.MapUtils.getInteger(map,"lmode");// 0:新增 1：修改 2=复诊预约计划
        Long lcbm01a = org.apache.commons.collections.MapUtils.getLong(map,"lcbm01a",0l);
        if (lmode==1 && (lcbm01a > 0)){
            mo = delclinicAdviceCBM01(map);
            if (StaticKeys.OH_FALSE.equals(mo.getResult())){
                return mo;
            }
        }

        map.put("tmpCBM", tmpCBM);
        map.put("tmpVAF",tmpVAF);
        map.put("tmpVBQ",tmpVBQ);
        
     
		map.put("tmpVAO",tmpVAO);
        map.put("tmpVAX",tmpVAX);
        map.put("tmpVBF",tmpVBF);
        insertCviCBM(map);// 插入医嘱单信息
        insertCviVbq(map);// 插入检查申请单信息
        insertCviVaf(map);// 插入医嘱内容记录信息
        List tmpVat = getTmpVat(tmpVAF);//获取vat病人手术记录信息
        updateTmpVAXAndVat(tmpVAX,tmpVat);//更新vat1中的vat01，更新vax中的vat01
        map.put("tmpVAX", tmpVAX);
        insertCviVao(map);// 插入病人诊断信息   only vao should be optimez
        insertCviVbg(map);// 插入病人医嘱状态信息    only vbg should be optimez
        updateCviBdj(map);// 更新私人诊疗项目信息     fuza maybe bhao optmz
        insertCviBdj(map); // 插入私人诊疗项目信息 fuza    maybe bhao optmz
        insertCviVax(map);// 插入医嘱手术人员信息和医嘱附件信息  only vax should be optimez
        //以下在发送时执行
//        updateVAT1(tmpVAF,tmpVBF, tmpVat);//更新vat1中的其它字段
//        List vavList = insertVAV(tmpVAF,tmpVBF, tmpVat);//插入vav信息
//        List<Long> vav01s = getNewId.nextId(vavList.size());
//        updateVAVList(vavList,vav01s);
        if (lmode!=2){updateCviVcj1(map);} //更新或修改挂号附表

        mo = new ModelVo(StaticKeys.OH_SUCCESS, "医嘱插入成功");
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        for(Map<String,Object> temp : tmpVAF)
        {
        	temp.put("vaa01", temp.get("vaa01")+"");
        	temp.put("cbm01", temp.get("cbm01")+"");
        	temp.put("vaf01", temp.get("vaf01")+"");
        	temp.put("vaf06", temp.get("vaf06")+"");
        	resultList.add(temp);
        }
        mo.setList(resultList);
        long l2 = System.currentTimeMillis();
        long b =(l2-l1);
        log.info("----------------------------------------------保存医嘱结束，耗时："+b+"豪秒------------------------------------------------------");
        return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            mo.setResultMsg(e.getMessage());
            return mo;
        }
    }
private FutureTask getCviTmpVAO_piplineMode(final JSONObject job, final List<Map<String, Object>> tmpCBM) {
	FutureTask<Object> FutureTask_getCviTmpVAO=new FutureTask( new Callable<Object>() {

		@Override
		public Object call()   {
			Object rzt = null;
			try {
				List tmpVAO = getCviTmpVAO(job,tmpCBM); // 获取病人诊断信息
				return tmpVAO;
			} catch (Exception e) {
				log.error(e);
			}
			return rzt;
		}
	}) ;
	//FutureTask_getCviTmpVAO.run();
	PiplineUtil.pool_ExecutorService_hiprid_lev1.submit(FutureTask_getCviTmpVAO);
	// AsynUtil.execMeth_Ays( FutureTask_getCviTmpVAO,"");
		return FutureTask_getCviTmpVAO;
	}

public static void main(String[] args)
{
    long l2 = System.currentTimeMillis();
    try
    {
        Thread.sleep(2000);
        long l3 = System.currentTimeMillis();
        System.out.println((l3-l2)/1000);
    }
    catch (InterruptedException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
//@//Cacheable(value = { "@Cachename1" })
    private List getvbfList(JSONObject job)
    {
        if (!job.containsKey("vbf1")) {
            return new ArrayList();
        }
        List ls = JsonUtil.parserToList(job.getJSONArray("vbf1"));
        int l = ls.size();
        Map map = null;
        for (int i = 0; i < l; i++) {
            map = (Map) ls.get(i);
            map.put("vaf01", 0);
        }
        return ls;
    }


    private void updateVAVList(List vavList, List<Long> vav01s) throws Exception
    {
        if (vavList!=null && vavList.size()>0)
        {
            for (int i = 0; i < vavList.size(); i++)
            {
                Map<String, Object> vav1 = (Map<String, Object>)vavList.get(i);
                vav1.put("vav01", vav01s.get(i));
            }
            Map<String, Object> temp = new HashMap<String, Object>();
            temp.put("vavList", vavList);
            cliNewAdviceDao.update("CliNewAdvice.updateVAVList", temp);
        }
    }

    private List insertVAV(List<Map<String, Object>> tmpVAF, List tmpVBF, List tmpVat) throws Exception
    {
        Map<String, Object> temp = new HashMap<String, Object>();
        temp.put("tmpVAF", tmpVAF);
        temp.put("tmpVBF", tmpVBF);
        return cliNewAdviceDao.queryForList("CliNewAdvice.insertVAV", temp);
    }

    private void updateVAT1(List<Map<String, Object>> tmpVAF, List tmpVBF, List tmpVat) throws Exception
    {
        Map<String, Object> temp = new HashMap<String, Object>();
        temp.put("tmpVAF", tmpVAF);
        temp.put("tmpVBF", tmpVBF);
        temp.put("tmpVat", tmpVat);
        cliNewAdviceDao.update("CliNewAdvice.updateVat1", temp);
    }

    private void updateTmpVAXAndVat(List tmpVAX,List tmpVAT)
    {
        
        for(int i=0;i<tmpVAT.size();i++)
        {
            Map<String, Object> vat1 = (Map<String, Object>)tmpVAT.get(i);
            vat1.put("vat01", Long.parseLong(vat1.get("vaf01").toString()));
        }
        for(Map<String, Object> vax1 : (List<Map<String, Object>>)tmpVAX)
        {
            for(int i=0;i<tmpVAT.size();i++)
            {
                Map<String, Object> vat1 = (Map<String, Object>)tmpVAT.get(i);
                if(vax1.get("vaf01").toString().equals(vat1.get("vaf01").toString()))
                {
                    vax1.put("vat01", vat1.get("vat01"));
                }
            }
        }
        
    }

    //vat1
    private List getTmpVat(List<Map<String, Object>> tmpVAF) throws Exception
    {
        Map<String, Object> temp = new HashMap<String, Object>();
        temp.put("tmpVAF", tmpVAF);
        return cliNewAdviceDao.queryForList("CliNewAdvice.queryVat1", temp);
    }

    //获取病人诊疗申请单
   // @//Cacheable(value = { "@Cachename1" })
    private List getCviTmpVBQ(JSONObject job) {
        if (!job.containsKey("vbq1")) {
            return new ArrayList<Map<String,Object>>();
        }
        List<Map<String,Object>> list = JsonUtil.parserToList(job.getJSONArray("vbq1"));
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = (Map<String,Object>) list.get(i);
            map.put("vbqid", 0);
        }
        return list;
    }

    /**
     * 根据医嘱单id删除医嘱 Clinic_Advice_CBM01_Del
     *
     * @param map
     * @return
     */
    public ModelVo delclinicAdviceCBM01(Map map)
    {
        /*
         * 新增医嘱修改状态信息 根据医嘱ID删除：1、诊断结果；2、诊疗信息；3、医嘱单；4、医嘱记录
         */

        ModelVo mv = new ModelVo();
        try
        {
            List<Long> ls = getNewId.nextId((int)(Math.random() * 100));
            map.put("VBG01", ls.get(0).intValue());
            // map.put("txt", "select wjProc()");
            cliNewAdviceDao.addVBG1(map);
            cliNewAdviceDao.delVAO1ByCBM01(map);
            cliNewAdviceDao.delVBQ1ByCBM01(map);

            cliNewAdviceDao.delCBM1ByCBM01(map);
            cliNewAdviceDao.delVAF1ByCBM01(map);

            mv.setResultMsg(StaticKeys.OH_SUCCESS);
            return mv;
        }
        catch (Exception e)
        {
            mv.setResultMsg(StaticKeys.OH_FALSE);
            return mv;
        }

    }

    private void insertCAM1(Map map)
            throws Exception
    {
        cliNewAdviceDao.insertCAM1(map);
    }

    private void insertCAH1(Map map)
            throws Exception
    {
        cliNewAdviceDao.insertCAM1(map);
    }

    @SuppressWarnings("unchecked")
    public ModelVo execClinicCAMInsert(Map map)
            throws Exception
    {
        ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
        // 添加疾病目录
        List camList = JsonUtil.getList(map.get("cam1").toString());
        map.put("camList", camList);
        List<Long> cam01List = getNewId.nextId(camList.size());
        //List<Long> cah01List = getNewId.nextId(camList.size());
        for(int i = 0;i<camList.size();i++)
        {
            Map temp = (Map)camList.get(i);
            temp.put("cam01", cam01List.get(i)+"");
            temp.put("cam03", cam01List.get(i)+"");
        }
        insertCAM1(map);
        // 添加疾病别名(没有别名)
        //insertCAH1(map);
        mo.setList(camList);
        return mo;
    }

    public ModelVo narcosisItemList(Map map)
            throws Exception
    {
        ModelVo mo = new ModelVo();
        List list = null;
        Object aDate = map.get("aDate");
        try
        {
            if (aDate == null || "".equals(aDate.toString()))
            {
                String date = DateUtils.getDateString(new Date());
                map.put(aDate, date);
            }
            list = cliNewAdviceDao.narcosisItemList(map);
        }
        catch (Exception e)
        {
            mo.setResultMsg(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setRows(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }

    public ModelVo checkOfficeProperty(Map map)
            throws Exception
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            list = cliNewAdviceDao.checkOfficeProperty(map);
        }
        catch (Exception e)
        {
            mo.setResultMsg(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setRows(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }

    public ModelVo getTreamentOffice(Map map)
            throws Exception
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            list = cliNewAdviceDao.getTreamentOffice(map);
        }
        catch (Exception e)
        {
        	e.printStackTrace();
            mo.setResultMsg(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setRows(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }

    public ModelVo getExamineType(Map map)
            throws Exception
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            Object lDate = map.get("lDate");
            if (lDate == null || "".equals(lDate.toString()))
            {
                map.put("lDate", lDate);
            }
            list = cliNewAdviceDao.getExamineType(map);
        }
        catch (Exception e)
        {
            mo.setResultMsg(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setRows(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }

    public ModelVo getOfficeById(Map map)
            throws Exception
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            list = cliNewAdviceDao.getOfficeById(map);
        }
        catch (Exception e)
        {
            mo.setResultMsg(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setRows(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }

    public ModelVo getVAL1(Map map)
            throws Exception
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = cliNewAdviceDao.getVAL1(map);
            mo.setList(list);
            mo.setRows(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            mo.setResultMsg(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo geiYaoTuJingList(Map map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = cliNewAdviceDao.geiYaoTuJingList(map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            mo.setResultMsg(StaticKeys.OH_FALSE);
            e.printStackTrace();
            return mo;
        }
    }

    public ModelVo zhenLiaoPinCiList(Map map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = cliNewAdviceDao.zhenLiaoPinCiList(map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            mo.setResultMsg(StaticKeys.OH_FALSE);
            e.printStackTrace();
            return mo;
        }
    }

    private ModelVo packModelVo(String nid,Map map){
        ModelVo mo = new ModelVo();
        List<?> list = null;
        try {
            list = cliNewAdviceDao.queryForList(nid,map);
            list = changeToString(list);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            mo.setTotal(list.size());
            mo.setRows(list);
        } catch (Exception e) {
            mo.setResult(StaticKeys.OH_FALSE);
            e.printStackTrace();
        }
        return mo;
    }
    private List<Map<String,String>> changeToString(List<?> list){
        List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
        for(Object o:list){
            if(o instanceof Map){
                Map<String,Object> pmap = (Map<String,Object>)o;
                Map<String,String> smap = new HashMap<String,String>();
                Iterator<Map.Entry<String, Object>> it = pmap.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry<String, Object> entry = it.next();
                    String str = String.valueOf(entry.getValue());
                    smap.put(entry.getKey(),str);
                }
                resultList.add(smap);
            }
        }
        return resultList;
    }
    /******手术附加项目******/
    public ModelVo selectOperationpro(){
        return packModelVo("CliNewAdvice.operationpro",null);
    }
    /******获取麻醉项目******/
    public ModelVo selectAnaesthesiaItem(){
        return packModelVo("CliNewAdvice.anaesthesiaItem",null);
    }
    public ModelVo zhongyaoyongfa()
    {
        return packModelVo("CliNewAdvice.zhongyaoyongfa",null);
    }

    public ModelVo zhongyaoJiaozhu(Map map)
    {
        return packModelVo("CliNewAdvice.zhongyaoJiaozhu",null);
    }

    public ModelVo getYongyaomudiList()
    {
        return packModelVo("CliNewAdvice.getYongyaomudiList",null);
    }

    public ModelVo shoushurenyuanList()
    {
        return packModelVo("CliNewAdvice.shoushurenyuanList",null);
    }
}